package com.cskaoyan.controller.marketingmanagement;

import com.cskaoyan.bean.marketingmanagementbean.IntegerArray;
import com.cskaoyan.bean.marketingmanagementbean.ReadVO;
import com.cskaoyan.bean.marketingmanagementbean.TopicData;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 创建日期: 2022/05/05 20:15
 *
 * @author liutao
 */

@RestController
@RequestMapping("admin/topic")
public class TopicController {

    @Autowired
    TopicService topicService;

    @RequestMapping("list")
    public BaseRespVo topicList(BaseParam baseParam, String title, String subtitle) {
        TopicData topicData = topicService.topicList(baseParam, title, subtitle);
        return BaseRespVo.ok(topicData);
    }

    // HttpMessageNotReadableException异常捕获
    @RequestMapping("create")
    public BaseRespVo topicCreate(@RequestBody MarketTopic marketTopic) {
        marketTopic.setAddTime(new Date());
        marketTopic.setUpdateTime(new Date());
        marketTopic.setDeleted(false);
        topicService.topicCreate(marketTopic);
        return BaseRespVo.ok(marketTopic);
    }



    @RequestMapping("read")
    public BaseRespVo topicRead(String id) {
        ReadVO readVO = topicService.topicRead(id);
        return BaseRespVo.ok(readVO);
    }

    @RequestMapping("delete")
    public BaseRespVo topicDelete(@RequestBody MarketTopic marketTopic) {
        marketTopic.setDeleted(true);
        topicService.topicDelete(marketTopic);
        return BaseRespVo.ok(null);
    }

    @RequestMapping("batch-delete")
    public BaseRespVo batchDelete(@RequestBody IntegerArray integerArray) {
        topicService.batchDelete(integerArray.getIds());
        return BaseRespVo.ok(null);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketTopic marketTopic) {
        topicService.update(marketTopic);
        MarketTopic res = topicService.queryTopic(marketTopic.getId());
        return BaseRespVo.ok(res);
    }
}
